45. 跳跃游戏 II
为保证权益,题目请参考 45. 跳跃游戏 II(From LeetCode).
解决方案1
CPP
C++
/*
* This is LeetCode 45 跳跃游戏 II
* ==============================
*
* Author: Keven Ge
* Date: 2020-05-04
*
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;
//
//class Solution {
//public:
// int jump(vector<int> &nums) {
// int ans = 0;
//// vector<int> dp(nums.size(), INT_MAX);
// int *dp = new int[nums.size()];
// fill(dp, dp + nums.size(), INT_MAX);
// dp[0] = 1;
// for (int i = 0; i <= nums.size() - 1; ++i) {
// if (dp[i] != INT_MAX) {
// for (int j = i + 1; j <= i + nums[i] && j <= nums.size() - 1; ++j) {
// dp[j] = min(dp[j], dp[i] + 1);
// }
// } else {
// break;
// }
// }
// return dp[nums.size() - 1] - 1;
// }
//};
class Solution {
public:
int jump(vector<int>& nums) {
int maxPos = 0, n = nums.size(), end = 0, step = 0;
for (int i = 0; i < n - 1; ++i) {
if (maxPos >= i) {
maxPos = max(maxPos, i + nums[i]);
if (i == end) {
end = maxPos;
++step;
}
}
}
return step;
}
};
int main() {
vector<int> vec;
vec.push_back(2);
vec.push_back(3);
vec.push_back(1);
vec.push_back(1);
vec.push_back(4);
Solution so;
cout << so.jump(vec) << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68